package com.cwvs.controller.admin;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cwvs.services.AppOrderService;
import com.cwvs.utils.AppConstants;
import com.cwvs.utils.Pages;

/**
 * 广告Controller
 * 
 * @author Administrator
 * 
 */
@Controller
@RequestMapping("/admin")
public class AdminAppOrderController {
	@Resource
	private AppOrderService appOrderService;

	@RequestMapping("/order_view")
	public ModelAndView view(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("app_order/app_order_list");
		return mv;
	}

	@RequestMapping("/order_list")
	public @ResponseBody
	Map<String, Object> list(HttpServletRequest request,
			HttpServletResponse response) {
		Pages page = new Pages();
		String limit = request.getParameter("limit");
		String start = request.getParameter("start");
		page.setPageSize(Integer.parseInt(limit));
		page.setCurrentPage(Integer.parseInt(start) / Integer.parseInt(limit)
				+ 1);
		HashMap<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("ORDER_DATE", request.getParameter("order_date"));
		paramsMap.put("ORDER_NO", request.getParameter("order_no"));
		paramsMap.put("ORDER_STATUS", request.getParameter("order_status"));
		paramsMap.put("CONTACTOR_PERSON", request.getParameter("contactor_person"));
		paramsMap.put("CONTACTOR_TEL", request.getParameter("contactor_tel"));
		Pages rePage = appOrderService.orderList(paramsMap, page);
		Map<String, Object> ret = new HashMap<String, Object>();
		ret.put("rows", rePage.getPage());
		ret.put("totalCount", rePage.getTotalRecord());
		return ret;
	}

	@RequestMapping("/order_edit")
	public ModelAndView storeEdit(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("app_order/app_order_edit");
		Map<String, Object> map = appOrderService.getOrderInfoById(request
				.getParameter("id"));
		// 预定订单
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(map, "TYPE_CODE"),
				"YDDD")) {
			mv.setViewName("app_order/app_order_book_edit");
		}
		// 外卖订单
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(map, "TYPE_CODE"),
				"WMDD")) {
			mv.setViewName("app_order/app_take_out_order_edit");
		}
		// 挂号订单
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(map, "TYPE_CODE"),
				"GHDD")) {
			mv.setViewName("app_order/app_register_order_edit");
		}
		// 同城订单
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(map, "TYPE_CODE"),
				"TCDD")) {
			mv.setViewName("app_order/app_same_city_order_edit");
		}
		// 老人专区(陪聊/买菜/打扫)
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(map, "TYPE_CODE"),
				"LRMC")
				|| StringUtils.equalsIgnoreCase(
						MapUtils.getString(map, "TYPE_CODE"), "LRPL")
				|| StringUtils.equalsIgnoreCase(
						MapUtils.getString(map, "TYPE_CODE"), "LRDS")) {
			mv.setViewName("app_order/app_arrondi_order_edit");
			
		}
		// 遛宠物
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(map, "TYPE_CODE"),
				"LCWDD")) {
			mv.setViewName("app_order/app_pet_products_order_edit");
			
		}
		// 陪跑订单
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(map, "TYPE_CODE"),
				"PPDD")) {
			mv.setViewName("app_order/app_with_run_order_edit");
		}
		// 时间工坊
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(map, "TYPE_CODE"),
				"SJGF")) {
			mv.setViewName("app_order/app_times_work_order_edit");
		}
		// 许愿盒子
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(map, "TYPE_CODE"),
				"XYHZ")) {
			mv.setViewName("app_order/app_promise_order_edit");
		}
		mv.addObject("model", map);
		return mv;
	}
	
	/**
	 * 订单价格处理
	 * @param request
	 * @param response
	 * @param file
	 * @return
	 */
	@RequestMapping("/order_update")
	public @ResponseBody
	Map<String, Object> orderUpdate(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String,Object> res=new HashMap<String,Object>();
		String id=request.getParameter("id");
		String sprice=request.getParameter("price");
		BigDecimal price=new BigDecimal(sprice);
		int status=AppConstants.STATUS_WAIT_PAY;
		String statusName=AppConstants.STATUS_WAIT_PAY_NAME;
		Map<String,Object> order=appOrderService.getOrderInfoById(id);
		if(order.isEmpty()){
			res.put("errormsg", "无法获取订单");
			res.put("success", false);
			return res;
		}
		if(MapUtils.getInteger(order, "STATUS")!=1){
			res.put("errormsg", "订单不允许修改");
			res.put("success", false);
			return res;
		}
		if(StringUtils.equalsIgnoreCase(MapUtils.getString(order, "TYPE_CODE"), "SJGF")){
			status=AppConstants.STATUS_WAIT;
			statusName=AppConstants.STATUS_WAIT_NAME;
		}
		
		boolean b=appOrderService.updateOrderStatusPrice(id, price,status , statusName);
		
		if(b){
			res.put("message", "订单处理成功");
		}else{
			res.put("errormsg", "订单处理失败");
		}
		res.put("success", b);
		return res;
	}
	
	/**
	 * 订单完成
	 * @param request
	 * @param response
	 * @param file
	 * @return
	 */
	@RequestMapping("/order_done")
	public @ResponseBody
	Map<String, Object> orderDone(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String,Object> res=new HashMap<String,Object>();
		String id=request.getParameter("id");
		int status=AppConstants.STATUS_DONE;
		String statusName=AppConstants.STATUS_DONE_NAME;
		Map<String,Object> order=appOrderService.getOrderInfoById(id);
		if(order.isEmpty()){
			res.put("errormsg", "无法获取订单");
			res.put("success", false);
			return res;
		}
		if(MapUtils.getInteger(order, "STATUS")!=4&&MapUtils.getInteger(order, "STATUS")!=2){
			res.put("errormsg", "不允许完成订单,订单状态:"+MapUtils.getString(order, "STATUS_NAME"));
			res.put("success", false);
			return res;
		}
		boolean b=appOrderService.updateStatus(id, status, statusName);
		if(b){
			res.put("message", "订单处理成功");
		}else{
			res.put("errormsg", "订单处理失败");
		}
		res.put("success", b);
		return res;
	}
	
	/**
	 * 订单取消
	 * @param request
	 * @param response
	 * @param file
	 * @return
	 */
	@RequestMapping("/order_cancel")
	public @ResponseBody
	Map<String, Object> orderCancel(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String,Object> res=new HashMap<String,Object>();
		String id=request.getParameter("id");
		int status=AppConstants.STATUS_CANCEL;
		String statusName=AppConstants.STATUS_CANCEL_NAME;
		Map<String,Object> order=appOrderService.getOrderInfoById(id);
		if(order.isEmpty()){
			res.put("errormsg", "无法获取订单");
			res.put("success", false);
			return res;
		}
		int st=MapUtils.getInteger(order, "STATUS");
		if(st!=1&&st!=2&&st!=3){
			res.put("errormsg", "不允许取消订单,订单状态:"+MapUtils.getString(order, "STATUS_NAME"));
			res.put("success", false);
			return res;
		}
		boolean b=appOrderService.updateStatus(id, status, statusName);
		if(b){
			res.put("message", "订单处理成功");
		}else{
			res.put("errormsg", "订单处理失败");
		}
		res.put("success", b);
		return res;
	}
	
	
}
